package com;

import com.context.AnnotationConfigApplicationContext;
import com.config.AppConfig;
import com.service.TimerService;
import java.util.concurrent.ThreadPoolExecutor;

public class Main {
    public static void main(String[] args) {
        try {
            // 创建上下文
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(AppConfig.class);

            // 获取Bean信息
            System.out.println("Bean数量: " + context.getBeanCount());
            System.out.println("Bean名称: " + context.getBeanNames());

            // 获取Bean
            TimerService timerService = context.getBean(TimerService.class);
            ThreadPoolExecutor executor = context.getBean(ThreadPoolExecutor.class);

            System.out.println("TimerService实例: " + timerService);
            System.out.println("ThreadPoolExecutor实例: " + executor);

            // 检查Bean是否成功获取
            if (timerService != null && executor != null) {
                System.out.println("开始执行定时任务...");

                // 启动时钟任务（每秒输出当前时间）
                timerService.startClock();

                // 启动倒计时任务（60秒倒计时）
                timerService.startCountdown(60);

                // 等待一段时间后关闭
                try {
                    Thread.sleep(65000); // 等待65秒
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }

                // 关闭线程池
                timerService.shutdown();
                System.out.println("程序执行完成！");
            } else {
                System.err.println("Bean获取失败！");
                if (timerService == null) {
                    System.err.println("TimerService为null");
                }
                if (executor == null) {
                    System.err.println("ThreadPoolExecutor为null");
                }
            }

            // 使用完成后销毁
            context.destroy();

        } catch (Exception e) {
            System.err.println("程序执行异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}